Ext.define("App.kpiconfiguration.view.KpiConfigurationList", {
    extend: "Extend.grid.CrudGridPanel",
    alias: 'widget.KpiConfigurationList',
    requires: ['App.kpiconfiguration.viewmodel.KpiConfigurationViewModel'],
    viewModel: 'kpi_configuration_viewmodel',
    config: {
        tbar: {
            quickCreate: {
                hidden: true
            }
        }
    },
    bind: {
        store: '{Query}',
        columns: '{columns}',
        search: '{search}'
    },
    editor: {
        formClass: 'App.kpiconfiguration.view.KpiConfigurationEditor',
        save: 'kpiconfiguration/save',
        get: 'kpiconfiguration/get_by_id',
        del: 'kpiconfiguration/deletes'
    },
    listeners: {
        // 查询关联的报表配置项
        cellclick: function ($this, td, cellIndex, record, tr, rowIndex, e, eOpts) {
            var me = this;
            if (td.textContent == '查看') {
                if (record.persistent != false) {
                    if (e.target.localName == 'a') {
                        Ext.create('App.kpiconfiguration.view.ViewContactReportsWindow', {extraParams: {'params.kpis.kpiConfiguration.id_in': record.get('id')}}).show();
                    }
                }
            }
        },
    },
    resetButtons: function (selected) {
        this.callParent(arguments);
        if (selected.length > 0) {
            var readOnlyData = Ext.Array.findBy(selected, function (item) {
                if (item.get('readOnly') == true) {
                    return true;
                }
                return false;
            })
            if (!Ext.isEmpty(readOnlyData) && app.hasRole('系统管理员') != true) {
                var btnEdit = this.down('button[action="edit"]').disable();
                var btnRemove = this.down('button[action="remove"]').disable();
            }
        }
    },
    editHandler: function (record, quickCreate, data) {
        if (record != null && record.get("readOnly") == true && app.hasRole('系统管理员') != true) {
            Extend.Msg.error('错误', "当前指标为只读指标，不允许修改！");
            return;
        }
        this.callParent(arguments);
    },
    // 删除之前检查删除想是否被引用
    deleteHandler: function (ids, messages) {
        var me = this;
        Extend.Msg.confirm('确认', '请确定您是否要删除选择的数据，删除后将不可恢复？', messages,
            function (opt) {
                if (opt === 'yes') {
                    Ext.Ajax.request({
                        url: 'report_configuration/query_simple',
                        params: {'params.kpis.kpiConfiguration.id_in': ids},
                        success: function (result, msg) {
                            var data = result.result.data;
                            if (data.length > 0) {
                                Ext.Msg.confirm('错误', '指标正在使用中，无法删除！ 是否查看关联报表？', function (btn) {
                                    if (btn == 'yes') {
                                        Ext.create('App.kpiconfiguration.view.ViewContactReportsWindow', {extraParams: {'params.kpis.kpiConfiguration.id_in': ids}}).show();
                                    }
                                });
                            } else {
                                me.doDelete(ids);
                            }
                        }
                    });
                }
            })
    }
});
Ext.define('App.kpiconfiguration.view.ViewContactReportsWindow', {
    extend: 'Extend.window.ModalWindow',
    requires: ["App.reportconfiguration.view.ReportConfigurationList"],
    alias: ['widget:ViewContactReportsWindow'],
    width: 900,
    height: 480,
    items: [{
        xtype: 'ReportConfigurationList',
        autoLoad: false,
        region: 'center',
        extraParams:function(){
            return {};
        },
        tbar: {
            add: {hidden: true},
            remove: {hidden: true}
        },
        listeners: {
            beforeload: function () {
                var me = this;
                var proxy = me.getStore().getProxy();
                proxy.setUrl('report_configuration/query_simple');
                var extraParams = proxy.getExtraParams();
                Ext.apply(extraParams, me.up().extraParams);
            }
        }
    }]
});